package com.warm.pump.module.work.controller;


import com.warm.pump.frame.gen.bean.PageBean;
import com.warm.pump.frame.gen.util.MyBeanUtils;
import com.warm.pump.frame.gen.util.OutJson;
import com.warm.pump.module.work.bean.po.gen.WorkParams;
import com.warm.pump.module.work.service.IWorkParamsService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * work_params
 */
@Controller
@RequestMapping("/workParams")
public class WorkParamsController {
    @Autowired
    private IWorkParamsService workParamsService;
//	@Autowired
//	private InstanceService instanceService;

    /**
     * 列表
     */
    @ResponseBody
    @RequestMapping("/list")
    @RequiresPermissions("workParams:list")
    public OutJson list(Integer page, Integer limit, WorkParams workParams, String paramKeyLike) {
        Map<String, Object> map = new HashMap<>();
        MyBeanUtils.copyBean2Map(map, workParams);
        if (StringUtils.isNotEmpty(paramKeyLike)) {
            map.put("paramKey_like", paramKeyLike);
        }
        map.put("orderBy", " update_time desc");
        if (StringUtils.isNotEmpty(workParams.getSidx()) && StringUtils.isNotEmpty(workParams.getOrder())) {
            map.put("orderBy", workParams.getSidx() + " " + workParams.getOrder());
        }
        //查询列表数据
        PageBean pageBean = workParamsService.getPageBeanByMap(map, page, limit);

        return OutJson.ok().put("page", pageBean);
    }


    /**
     * 信息
     */
    @ResponseBody
    @RequestMapping("/info/{id}")
    @RequiresPermissions("workParams:info")
    public OutJson info(@PathVariable("id") Long id) {
        WorkParams workParams = workParamsService.queryObject(id);

        return OutJson.ok().put("workParams", workParams);
    }


    /**
     * 保存
     */
    @ResponseBody
    @RequestMapping("/save")
    @RequiresPermissions("workParams:save")
    public OutJson save(@RequestBody WorkParams workParams) {
        workParams.setCreateTime(new Date());
        workParams.setUpdateTime(new Date());
        workParamsService.save(workParams);

        return OutJson.ok();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("workParams:update")
    public OutJson update(@RequestBody WorkParams workParams) {
        workParams.setUpdateTime(new Date());
        workParamsService.update(workParams);

        return OutJson.ok();
    }

    /**
     * 删除
     */
    @ResponseBody
    @RequestMapping("/delete")
    @RequiresPermissions("workParams:delete")
    public OutJson delete(@RequestBody Long[] ids) {
        workParamsService.deleteBatch(ids);

        return OutJson.ok();
    }

    @ResponseBody
    @RequestMapping("/execReferWork")
    public OutJson execReferTask(Long workId, Long paramsId) {
        workParamsService.createInstByWorkIdAndParamsId(workId, paramsId);
        return OutJson.ok();
    }

}
